{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f314fc67-f8c5-4435-9ed7-c49316aaa01c",
   "metadata": {},
   "source": [
    "## enumerate函数\n",
    "enumerate() 函数用于将一个可遍历的数据对象(如**列表、元组或字符串**)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5cc719f9-37ec-41eb-bedb-34c8295d1e7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<enumerate object at 0x000001CC09F81C60>\n",
      "[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\n"
     ]
    }
   ],
   "source": [
    "seasons = ['Spring', 'Summer', 'Fall', 'Winter']\n",
    "print(enumerate(seasons)) #打印一个地址\n",
    "print(list(enumerate(seasons)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2f5c558-3a58-4285-8955-d42e1ae3226c",
   "metadata": {},
   "source": [
    "## 比较常见的作法是 反转键值对\n",
    "比方说,对于上面的例子,键是0,1,2,3;值是Spring,Summer,Fall,Winter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f7c71e44-9060-4e85-8707-9b5aec8c661c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 'Spring', 1: 'Summer', 2: 'Fall', 3: 'Winter'}\n",
      "{0: 'Spring', 1: 'Summer', 2: 'Fall', 3: 'Winter'}\n"
     ]
    }
   ],
   "source": [
    "print(dict(enumerate(seasons)))\n",
    "print( {k:v for k,v in enumerate(seasons) } )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8f4b9de8-b92a-4c38-a4e6-443d1a5d0702",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'I': 0, 'will': 1, 'miss': 2, 'your': 3, 'bright': 4, 'eyes': 5, 'and': 6, 'sweet': 7, 'smile': 8}\n"
     ]
    }
   ],
   "source": [
    "texts = \"I will miss your bright eyes and sweet smile\"\n",
    "word_list = texts.split(\" \")\n",
    "tokens = {v: k for k,v in enumerate(word_list)}\n",
    "print(tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ce5dd044-c6a8-48c6-80e4-4cc16a3cd915",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Users\\MECHREVO\\anaconda3\\Lib\\site-packages\\torch\\utils\\_pytree.py:185: FutureWarning: optree is installed but the version is too old to support PyTorch Dynamo in C++ pytree. C++ pytree support is disabled. Please consider upgrading optree using `python3 -m pip install --upgrade 'optree>=0.13.0'`.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.6.0+cpu\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(torch.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6e8a7df9-8268-4f59-8c39-e4ad1d24fdfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.cuda.is_available()\n",
    "N = 1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "881de181-09d7-4b16-ac24-1c1c9ab6d4c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "439 μs ± 10.4 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "N = 1000\n",
    "x = torch.randn(N,N).to(torch.float16).to(\"cuda\")\n",
    "%timeit (x @ x).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7282808e-c899-404b-bded-5b31463bde91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.43 ms ± 142 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "y = torch.randn(N,N)\n",
    "%timeit (y @ y).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3a5c0338-840c-4db1-a339-5e8c6ca43386",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float16"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.dtype"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
